package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Adultertype;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("AdultertypeDAO")
@Transactional
public class AdultertypeDAOImpl extends AbstractJpaDao<Adultertype> implements AdultertypeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Adultertype.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public AdultertypeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Adultertype findAdultertypeById(Integer id) throws DataAccessException {

		return findAdultertypeById(id, -1, -1);
	}

	@Transactional
	public Adultertype findAdultertypeById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdultertypeById", startResult, maxRows, id);
			return (Adultertype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Adultertype> findAdultertypeByName(String name) throws DataAccessException {

		return findAdultertypeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adultertype> findAdultertypeByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdultertypeByName", startResult, maxRows, name);
		return new LinkedHashSet<Adultertype>(query.getResultList());
	}

	@Transactional
	public Set<Adultertype> findAdultertypeByNameContaining(String name) throws DataAccessException {

		return findAdultertypeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adultertype> findAdultertypeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdultertypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Adultertype>(query.getResultList());
	}

	@Transactional
	public Set<Adultertype> findAdultertypeByDescription(String description) throws DataAccessException {

		return findAdultertypeByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adultertype> findAdultertypeByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdultertypeByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Adultertype>(query.getResultList());
	}

	@Transactional
	public Set<Adultertype> findAdultertypeByDescriptionContaining(String description) throws DataAccessException {

		return findAdultertypeByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adultertype> findAdultertypeByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findAdultertypeByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Adultertype>(query.getResultList());
	}

	@Transactional
	public Adultertype findAdultertypeByPrimaryKey(Integer id) throws DataAccessException {
		return findAdultertypeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Adultertype findAdultertypeByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdultertypeByPrimaryKey", startResult, maxRows, id);
			return (Adultertype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Adultertype> findAdultertypesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdultertypesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Adultertype> findAllAdultertypes() throws DataAccessException {

		return findAllAdultertypes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adultertype> findAllAdultertypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAdultertypes", startResult, maxRows);
		return new LinkedHashSet<Adultertype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Adultertype> findAdultertypeByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Adultertype>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Adultertype> findAdultertypesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Adultertype.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Adultertype> adultertypes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return adultertypes;
	}

	@Transactional
	public Adultertype findAdultertypeByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findAdultertypeByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Adultertype findAdultertypeByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findAdultertypeByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Adultertype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Adultertype entity) {
		return true;
	}
}
